package.path = package.path ..';../?.lua'
_G.EXPORT_ASSERT_TO_GLOBALS = true
require('lib/luaunit')
local buffLib = require('buff/buffLib')
local publish = require('publish/publish')


_G.testBuffFlow = {}

function testBuffFlow:setUp()

end

function testBuffFlow:test_aBuffLiveCircle_scriberIsManager()

    local buffManager = buffLib.buffManager:New()
    local buff = buffLib.buff:New()
    local battleEventCenter = publish.provider:New()
    buffManager.subscriber = publish.subscriber:New('buffSubcriber',battleEventCenter)
    buffManager.subscriber:subscribe(publish.msgType.Skill)
    buffManager:addBuff(buff)
    local publisher = publish.publisher:New()
    publisher:addMsgCenter(battleEventCenter)

    local tbSKillMsg = {}
    local msg1 = publish.Msg:New(0,publish.msgType.Skill,tbSKillMsg)
    publisher:publish(msg1)

end


function testBuffFlow:test_aBuffLiveCircle_scriberIsBuff()

    local buffManager = buffLib.buffManager:New()
    local buff = buffLib.buff:New()
    local battleEventCenter = publish.provider:New()
    buffManager:addBuff(buff)
    buff.subscriber = publish.subscriber:New('buffSubcriber',battleEventCenter)
    buff.subscriber:subscribe(publish.msgType.Skill)
    buff.subscriber.tbMsg = {}
    buff.subscriber.getMsg = function(aSubscriber,msg)
        table.insert(aSubscriber.tbMsg, msg)
    end
    local publisher = publish.publisher:New()
    publisher:addMsgCenter(battleEventCenter)

    local tbSKillMsg = {}
    local msg1 = publish.Msg:New(0,publish.msgType.Skill,tbSKillMsg)
    publisher:publish(msg1)

    local tStart = os.time()
    local tInit = os.time()
    local timeInterval = 1
    for i=1 ,10000 do
        local tNow = tStart + i
        if tNow - tInit > 1 then
            tInit = tNow
            battleEventCenter:update(tNow, timeInterval)
        end
    end

    assertTrue(#buff.subscriber.tbMsg,3)
    assertTrue(buff.subscriber.tbMsg[1] == msg1)

end

local lu = LuaUnit.new()
lu:setOutputType("tap")
os.exit( lu:runSuite() )
